package com.yiip.mr.flowsum;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class FlowSumDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    	
    	args = new String[2];
    	args[0] = "E:/bigDataDemo/flowsum/input";
    	args[1] = "E:/bigDataDemo/flowsum/output";

        //1、job加载配置
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //2、设置job的class路径
        job.setJarByClass(FlowSumDriver.class);

        //3、设置mapper和reduce
        job.setMapperClass(FlowSumMapper.class);
        job.setReducerClass(FlowSumReducer.class);

        //4、设置输入参数类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowBean.class);

        //5、设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //这里属于切片机制优化，小文件用CombineTextInputFormat，来合并小文件，成1个切片，不然浪费性能
        job.setInputFormatClass(CombineTextInputFormat.class);
        //同时设置切片大小为4m，如果注释掉这两行，那么切片数量变成4.
        CombineTextInputFormat.setMaxInputSplitSize(job,4*1024*1024);

        //6、提交job
        boolean result = job.waitForCompletion(true);
        
        System.exit(result ? 0 : 1);
    }
}
